<extend name="Public/common" />
<block name="body">
	<div class="bought-goods bg-wbp">
		<div class="buy-goods">
			<img src="{:thumb($order['goods_image'], 680, 90, 3)}" />
			<p class="font23 ">
				{$order['goods_name']} <br />
				<img src="__IMG__/b.png" />预计<span class="font23 ">{:date('m月d日 H:i', $order['start_deliver_time'])} - {:date('H:i', $order['end_deliver_time'])}</span>送达
			</p>
		</div>
	</div>
	<div class="bought-add bg-wbp">
		<input type="hidden" id="region_id" value="{$address['region_id']}" />
		<input type="text" class="bought-add-nam frame-style" placeholder="姓名" id="member_name" name="member_name" value="{$address['member_name']}" />
		<input type="text" class="bought-add-ph frame-style" placeholder="电话" id="member_phone" name="member_phone" value="{$address['member_phone']}" />
		<input type="text" class="bought-add-di frame-style" id="region_name" name="region_name" value="{$address['region_name']}" readonly />
		<input type="text" class="bought-add-postAdd frame-style" placeholder="请输入详细地址（楼层或门牌号）"  id="address" name="address" value="{$address['address']}" />
	</div>
	<img src="__IMG__/addressicon.png" class="bought-add-img" />
	<div class="bought-money bg-wbp">
		<div class="bought-money-pri frame-style">
			<span class="col-g font23 ">套餐价格</span><span class="fr col-r font23">{$order['buy_price']}元</span>
		</div>
		<div class="bought-money-chit frame-style">
			<a><span class="col-g font23 ">代金券</span></a><span class="fr col-r font23 "><empty name="order.coupon_sn">无<else />-{$order['coupon_value']}元</empty></span>
		</div>
		<div class="bought-money-vip frame-style clearfix">
			<span class="col-g font23">会员福利</span><empty name="order.vip"><span class="bought-nvip">您还不是会员</span><else /><span class="fr col-r font23 ">-{$order['member_vip']}元</span></empty>
		</div>
	</div>
	<div class="bought-pay frame-style">
		<span class="bought-pay-btn bg-r fr font30" id="payBtn">支付</span>
		<span class="col-r fr font30 bought-pay-price">{$order['pay_price']}元</span>
		<span class="col-g fr font23 ">支付金额:</span>
	</div>
</block>
<block name="js">
	<script src="__WECHAT__/js/main.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		/* 支付验证 */
		function IsChinese(name) {
			var str = $(name).val();
			reg = /^([\u4e00-\u9fa5]){1,7}$/; //只能是中文，长度为2-7位
			if(!reg.test(str)) {
				salert("对不起，您输入的姓名不正确!");
				return false;
			}else{
				return true;
			}
		}
		function IsPhone(name) {
			var str = $(name).val();
			reg =/^1[3,4,5,7,8]\d{9}$/; //是不是手机号码
			if(!reg.test(str)) {
				salert("对不起，您输入的手机号码不正确!");
				return false;
			}else{
				return true;
			}
		}
		function IsAddress(name) {
			var str = $.trim($(name).val());
			if(str=='') {
				salert("对不起，请您您输入地址!");
				return false;
			}else{
				return true;
			}
		}
		//姓名验证
		$(".bought-add-nam").on("blur",function(){
			IsChinese(".bought-add-nam");
		})
		//手机号验证
		$(".bought-add-ph").on("blur",function(){
			IsPhone(".bought-add-ph");
		})
		//地址标签的隐藏
		$(".bought-add-postAdd").on("input",function(){
			if($(".bought-add-postAdd").val()!=""){
				$(".bought-add-img").css("display","none");
			}else{
				$(".bought-add-img").css("display","block");
			}
		});
		$(".bought-add-postAdd").on("blur",function(){
			IsAddress(".bought-add-postAdd");
		});

		//点击支付
		$('#payBtn').click(function(){
			if(IsChinese(".bought-add-nam")&&IsPhone(".bought-add-ph")&&IsAddress(".bought-add-postAdd")){
				$.ajax({
					url: "{:C('SITE_URL')}wechat/index/submitOrder",
					dataType: "json",
					data: {
						"region_id": $("#region_id").val(),
						"member_name": $("#member_name").val(),
						"member_phone": $("#member_phone").val(),
						"region_name": $("#region_name").val(),
						"address": $("#address").val()
					},
					type: "POST",
					beforeSend: function() { salert("订单支付中"); },
					success: function(data) {
						if(data.code == 0){
							callpay(data.data);
						} else if (data.code == 999){
							window.location = "{:C('SITE_URL')}wechat/index/finnshedOrder";
						} else {
							salert(data.msg);
						}
					},
//					complete: function() { },
					error: function() {
						salert("订单支付失败，请重新支付");
					}
				});
			}
		});

		function jsApiCall(data){
			WeixinJSBridge.invoke(
					'getBrandWCPayRequest',
					data,
			function(res){
				//WeixinJSBridge.log(res.err_msg);
				//alert(res.err_code+res.err_desc+res.err_msg);
				if(res.err_msg == "get_brand_wcpay_request:ok"){
					window.location = "{:C('SITE_URL')}wechat/index/finnshedOrder";
				} else {
					salert("支付取消，请重新支付");
				}
			}
		);
		}

		function callpay(data){
			if (typeof WeixinJSBridge == "undefined"){
				if( document.addEventListener ){
					document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
				}else if (document.attachEvent){
					document.attachEvent('WeixinJSBridgeReady', jsApiCall);
					document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
				}
			}else{
				jsApiCall(data);
			}
		}
	</script>
</block>